<template>
    <view>

		<view class="address_wrap" 
			v-for="(item,index) in addressList"
			:key="index"
			>
			
				<view class="iconfont icon-shouhuodizhi">
					
				</view>
				<view class="center_wrap" :data-id="item.mobile" @click="handleAddress">
				<view class="info_wrap">
					<view class="info">{{item.consignee}}</view>
					<view class="tel">{{item.mobile}}</view>
				</view>
				<view class="address">{{item.address+item.detailAddress}}</view>
				</view>
				<view class="clear" :data-id="item.mobile" @click="ckear">删除</view>
		</view>
		<view class="add_btn" @click="add">+ 点击添加收货地址</view>
		<view class="add_btn" @click="local">+ 一键定位</view>
	</view>	
</template>

<script>

	export default {

		data(){
			return{
				addressList:[],
				checkNum:0
			}
		},
		onLoad(){
			this.addressList = uni.getStorageSync('addressList')
		},
		onShow(){
			// uni.showToast({title:'只能选择一个地址',icon:'none',duration:3000})
		},
        methods:{
		    add(){
			    uni.navigateTo({url:'/pages/add_userinfo/index'})
		    },

			handleAddress(e){

					
					//获取收货地址数组
					let addressList = uni.getStorageSync('addressList')||[];

					const mobile_id =e.currentTarget.dataset.id;
						
					// 找到商品对象索引
					let index = addressList.findIndex(v=>v.mobile === mobile_id);
					let info = addressList[index];

					let address=uni.getStorageSync('address')||{};
					address.userName = info.consignee;
					address.telNumber = info.mobile;
					address.all = info.address+info.detailAddress;
					uni.setStorageSync('address',address);	

					uni.switchTab({url:'/pages/cart/index'})
					
			},
			ckear(e){
				let addressList = uni.getStorageSync('addressList')||[];

				const mobile_id =e.currentTarget.dataset.id;
						
				// 找找到商品对象索引
				let index = addressList.findIndex(v=>v.mobile === mobile_id);

				addressList.splice(index,1);
				uni.setStorageSync('addressList',addressList);

				uni.showToast({title:'删除成功',duration:2000,success:()=>{
					setTimeout(()=>{
						uni.switchTab({url:'/pages/user/index'})
					},2000)
					
				}})

			},
			local(){
				uni.chooseAddress({
					success: (result) => {
						let address = result;
						address.all = address.provinceName+address.cityName+address.countyName+address.detailInfo;
						uni.setStorageSync('address',address);
					}
				});
				uni.switchTab({url:'/pages/cart/index'})
			}
	    }
		
	}
</script>

<style lang="scss">

	.address_wrap {
		display: flex;
		border: 1rpx solid #ccc;
		padding: 15rpx;
	.iconfont {
		flex: 1;
		padding-top: 35rpx;
		color: var(--themeColor);
		font-size: 50rpx;
		
	}

	.center_wrap{
		flex: 5;
		
		.info_wrap {
			display: flex;
	
			.info {
				flex: 1;

			}

			.tel {
				flex: 4;

			}
		}

		.address {

			padding-top: 10rpx;

		}
	}
	

	.clear {
		padding-top: 20rpx;
		flex: 1;
		color: #e64340;

	}
	}
	.add_btn{
		margin: 50rpx 220rpx 0 220rpx;
		padding: 20rpx;
		border: 1rpx solid var(--themeColor);
		border-radius: 10rpx;
		text-align: center;
	}
	
</style>